<?php

/**
 * 批量用表格添加数据
 */

namespace app\fuck\model;

use think\Db;
use think\Model;

class WhzxModel extends Model
{
    protected $pk = 'id';
    protected $name = '';
    protected $type = ['more' => 'array',];
    protected $autoWriteTimestamp = true;   //自动写入时间戳

    /**
     * 高一，将老师表里的老师写入数课表基础数据表
     */
    public function save_ls($d, $njmc, $xxmc,  $xnxq, $zc, $xq)
    {
        $num = 0;
        Db::startTrans();
        try {
            foreach ($d as $k => $v) {
                $w['xxmc'] = '武湖中学';
                $w['xnxq'] = $xnxq;
                $w['njmc'] = $njmc;
                $w['lsxm'] = $v;
                $id = Db::name('kb_base_ls')->where($w)->value('id');
                if (empty($id)) {
                    $data = ['xxmc' => $xxmc, 'type' => 1, 'lsxm' => $v, 'xnxq' => $xnxq, 'njmc' => $njmc, 'create_time' => time()];
                    $id = Db::name('kb_base_ls')->insert($data, true); //直接替换->insertGetId($data);
                    if ($id > 0) $num++;
                } else {
                    $data = ['xxmc' => $xxmc, 'type' => 1, 'lsxm' => $v, 'xnxq' => $xnxq, 'njmc' => $njmc, 'update_time' => time()];
                    $id = Db::name('kb_base_ls')->where('id', $id)->update($data); //直接替换->insertGetId($data);
                }
            }
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            var_dump($e);
            return false;
        }

        return $num;
    }

    /**
     * 高一，将老师表里的任课老师写入数课表基础数据表
     */
    public function save_kc_ls($d, $njmc, $xxmc, $xnxq, $zc, $xq)
    {
        //var_dump("老师表数据：");
        //var_dump($d);
        $num = 0;
        Db::startTrans();
        try {
            foreach ($d as $k1 => $v1) {    //遍历行
                if (empty($v1)) continue;
                $bjmc = $v1['bj']; //班级名称
                foreach ($v1 as $k2 => $v2) {
                    if ($k2 == "bj" || $k2 == "bzr" || $k2 == "jswz") continue;

                    $data = ['xxmc' => $xxmc, 'xnxq' => $xnxq, 'njmc' => $njmc, 'bjmc' => $bjmc, 'kcmc' => $k2, 'lsxm' => $v2, 'create_time' => time()];
                    $id = Db::name('kb_kc_ls')->where("xxmc='$xxmc' and xnxq='$xnxq' and njmc='$njmc' and bjmc='$bjmc' and kcmc='${k2}' ")->value('id');
                    if (empty($id)) {
                        $id = Db::name('kb_kc_ls')->insert($data, true); //直接替换->insertGetId($data);
                        if ($id > 0) $num++;
                    } else {
                        $id = Db::name('kb_kc_ls')->where('id', $id)->update($data);
                    }
                }
            }
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            var_dump($e);
            return false;
        }

        return $num;
    }

    /**
     * 高一，转换并写入课表，单个单元格分析
     */
    public function save_kb_bj($d, $xxmc, $njmc, $xnxq)
    {
        Db::startTrans();
        try {
                //取得各字段id号
                if (empty($d)) return false;    //第一行是空行
                if (empty($d['kcmc'])) return false; //课程名称为空则不处理
                if (empty($d['kcmc']) == '班主任') return false;
        
                $ret = [];
        
                var_dump("一行课表上青天：");
                var_dump($d);
        
                $d['njmc'] = $njmc;
                $d['xxmc'] = $xxmc;
        
                $zc_ar = $this->zf_zc1($d['zc']);
                $d['zc'] = implode(",", $zc_ar);
                var_dump('zc',$d['zc']);
        
                //求kc_mc的老师
                //'语文', '数学', '英语', '物理', '化学', '生物', '政治', '历史', '地理', '体育', '音乐', '美术', '信息', '通用技术'
                $where['xxmc'] = $xxmc;
                $where['xnxq'] = $xnxq;
                $where['njmc'] = $njmc;
                var_dump($where);
                $kc = Db::name('kb_base_kc')
                ->whereRaw('xxmc=:xxmc and xnxq=:xnxq and njmc=:njmc and (kczs=:kczs or kcjc=:kcjc)',
                    ['xxmc'=>$xxmc, 'xnxq'=>$xnxq,'njmc'=>$njmc,'kczs' => $d['kcmc'], 'kcjc' => $d['kcmc']])
                ->find();
                if (empty($kc)) {
                    throw new \Exception("从课程表取数据为空",$kc);
                }
                $d['kc_id'] = $kc['bh'];
                $d['kcmc'] = $kc['kczs'];
                $kcbs = $kc['kcmc'];
                var_dump("\n");
                var_dump('kcbs:',$kc);
        
                $w = $where;
                $w['bjmc'] = $d['bjmc'];
                $w['kcmc'] = $kcbs;
                $v_lsxm = Db::name('kb_kc_ls')->where($w)->value('lsxm');
                if (empty($v_lsxm)) {
                    throw new \Exception("从kb_kc_ls表取数据为空");
                }
                $d['lsxm'] = $v_lsxm;
                var_dump('lsxm',$v_lsxm);
                
                $w = $where;
                $w['lsxm'] = $v_lsxm;
                $v_lsbh = Db::name('kb_base_ls')->where($w)->value('bh');
                if (empty($v_lsbh)) {
                    throw new \Exception("从kb_base_ls表取数据为空");
                }
                $d['ls_id'] = $v_lsbh;
                var_dump('ls_id:',$v_lsbh);
        
                $w = $where;
                $w['bjjc'] = $d['bjmc'];
                $v_bj = Db::name('kb_base_bj')->where($w)->find();
                if (empty($v_bj)) {
                    throw new \Exception("从表取数据为空");
                }
                var_dump("v_bj:");
                var_dump($v_bj);
                $d['bjmc'] = $v_bj['bjmc'];
                $d['bj_id'] = $v_bj['bj_id'];
                $d['csmc'] = $v_bj['csmc'];
                $d['csbm'] = $v_bj['csbm'];

                $d['create_time'] = time();

                $w1 = [];
                $w1['xxmc'] = $xxmc;
                $w1['xnxq'] = $xnxq;
                $w1['njmc'] = $njmc;
                $w1['bjmc'] = $d['bjmc'];
                $w1['xq'] = $d['xq'];
                $w1['jc'] = $d['jc'];
                $id = Db::name('kb_bj_kb')->where($w1)->value('id');
                $fields = "xxmc,xnxq,zc,xq,jc,njmc,bjmc,kcmc,lsxm,bj_id,kc_id,ls_id,csmc,csbm,create_time";
                if (empty($id)) {
                    Db::name('kb_bj_kb')->field($fields)->insertGetId($d, true);
                } else {
                    $id = Db::name('kb_bj_kb')->field($fields)->where('id', $id)->update($d);
                }

            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return ["code" => 0, "data" => $d, "msg" => $e->getMessage()];
        }

        return ["code" => 1, "data" => $d, "msg" => "提交成功"];;
    }

    /**
     * 分析1-23,24,26这样的以逗号分割的周次，不分单双周 
     */
    public function zf_zc1($zc)
    {
        $v_zc = [];
        $ar = explode(",", $zc);
        foreach ($ar as $k => $v) {
            $ok = strpos($v, "-");
            if ($ok) {
                $a = explode("-", $v);
                $c1 = intval($a[0]);
                $c2 = intval($a[1]);
                for ($i = $c1; $i <= $c2; $i++) {
                    $v_zc[] = $i;
                }
            } else {
                if (is_numeric($v)) $v_zc[] = intval($v);
            }
        }


        return $v_zc;
    }

    /**
     * 分析1-23,24,26这样的以逗号分割的周次，分单双周返回
     */
    public function zf_zc($zc)
    {
        $v_zc = [];
        $ar = explode(",", $zc);
        foreach ($ar as $k => $v) {
            $ok = strpos($v, "-");
            if ($ok) {
                $a = explode("-", $v);
                $c1 = intval($a[0]);
                $c2 = intval($a[1]);
                for ($i = $c1; $i <= $c2; $i++) {
                    $v_zc[] = $i;
                }
            } else {
                if (is_numeric($v)) $v_zc[] = intval($v);
            }
        }

        //分单双周
        $zc_dz = [];
        $zc_sz = [];
        foreach ($v_zc as $k => $v) {
            if ($v % 2 == 1) {
                //奇数
                $zc_dz[] = $v;
            } else if ($v % 2 == 0) {
                //偶数
                $zc_sz[] = $v;
            }
        }

        return [
            //"zc" => $v_zc,
            "dz" => $zc_dz,
            "sz" => $zc_sz,
        ];
    }
}
